package pcb.views 
{
	import com.intel.core.UIComponent;
	import com.intel.utils.Debugger;
	import com.intel.utils.TextRender;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import pcb.models.LocaleModel;
	import pcb.views.library.Checkbox;
	import pcb.views.uievents.UIEvent;
	
	/**
	 * NavGUI holds the presentation logic for the button controls defined in main-standalone.fla
	 * @author Fintan Boyle
	 */
	public class NavGUI extends UIComponent implements ILocalise 
	{
		private var quitBut:MovieClip;
		private var prevBut:MovieClip;
		private var nextBut:MovieClip;
		private var playBut:MovieClip;
		private var stopBut:MovieClip;
		private var replayBut:MovieClip;
		private var muteBut:MovieClip;
		private var voTextBut:MovieClip;
		private var showMeBut:MovieClip;
		private var legalLink:MovieClip;
		private var autoTransitionMc:MovieClip;
		private var autoTransitionChkBox:Checkbox;
		
		private var quitButTxt:String = "Quit";
		private var prevButTxt:String = "Previous";
		private var nextButTxt:String = "Next";
		private var pauseButTxt:String = "Pause";
		private var playButTxt:String = "Play";
		private var replayButTxt:String = "Replay";
		private var stopButTxt:String = "Stop";
		private var muteButTxt:String = "Mute";
		private var unmuteButTxt:String = "UnMute";
		private var voOnButTxt:String = "Show text";
		private var voOffButTxt:String = "Hide text";
		private var autoPlayButTxt:String = "Autoplay";
		private var showMeButTxt:String = "Show me";
		
		//simple presentation model
		private var isPaused:Boolean = false;
		private var isMuted:Boolean = false;
		private var isShowingVOText:Boolean = true;
		private var isAutoTransitioning:Boolean;
		private var onFirstSlide:Boolean;
		private var onLastSlide:Boolean;
		
		
		
		public function NavGUI() 
		{
			
		}
		
		override protected function addChildren():void
		{
			quitBut = this.getAppRoot().getChildByName("mcQuit") as MovieClip;
			prevBut = this.getAppRoot().getChildByName("mcPrv") as MovieClip;
			nextBut = this.getAppRoot().getChildByName("mcNxt") as MovieClip;
			playBut  = this.getAppRoot().getChildByName("mcPause") as MovieClip;
			stopBut  = this.getAppRoot().getChildByName("mcStop") as MovieClip;
			replayBut  = this.getAppRoot().getChildByName("mcReplay") as MovieClip;
			muteBut  = this.getAppRoot().getChildByName("mcAudio") as MovieClip;
			voTextBut  = this.getAppRoot().getChildByName("voTextBut") as MovieClip;
			showMeBut  = this.getAppRoot().getChildByName("showMeBut") as MovieClip;
			legalLink  = this.getAppRoot().getChildByName("mcLegalLink") as MovieClip;
			if(legalLink){
				legalLink.buttonMode = true;
				legalLink.useHandCursor = true;
				legalLink.mouseChildren = false;
			}
			
			autoTransitionMc = this.getAppRoot().getChildByName("playthroughChkBox") as MovieClip;
			
			if(autoTransitionMc){
				autoTransitionChkBox = new Checkbox(autoTransitionMc, autoPlayButTxt);
			}
			
			preInitialise();
			createListeners();
		}
		
		public function localiseLabels(localeModel:LocaleModel):void 
		{
			if(quitButTxt)quitButTxt = localeModel.getLabelbyId("quit_button");
			if(prevButTxt)prevButTxt = localeModel.getLabelbyId("prevSlide_button");
			if(nextButTxt)nextButTxt = localeModel.getLabelbyId("nextSlide_button");
			if(pauseButTxt)pauseButTxt = localeModel.getLabelbyId("pause_button");
			if(playButTxt)playButTxt = localeModel.getLabelbyId("play_button");
			if(replayButTxt)replayButTxt = localeModel.getLabelbyId("replay_button");
			if(stopButTxt)stopButTxt = localeModel.getLabelbyId("stop_button");
			if(muteButTxt)muteButTxt = localeModel.getLabelbyId("mute_button");
			if(unmuteButTxt)unmuteButTxt = localeModel.getLabelbyId("unmute_button");
			if(voOnButTxt)voOnButTxt = localeModel.getLabelbyId("showText_button");
			if(voOffButTxt)voOffButTxt = localeModel.getLabelbyId("hideText_button");
			if(autoPlayButTxt)autoPlayButTxt = localeModel.getLabelbyId("autoPlay_button");
			if(showMeButTxt)showMeButTxt = localeModel.getLabelbyId("showMe_button");
			
			if(showMeBut) {
				TextRender.createLabel(showMeBut.txt_label, showMeButTxt);
				//showMeBut.txt_label.text = showMeButTxt;
			}
			
			if(autoTransitionChkBox) {
				//TextRender.createLabel(autoTransitionChkBox.label, autoPlayButTxt);
				autoTransitionChkBox.label = autoPlayButTxt;
			}
		
			if(legalLink){ 
				TextRender.createLabel(legalLink.txt_label, localeModel.getLabelbyId("legal_agreement"));
			}
		}
		
		public function createListeners():void 
		{
			if (quitBut) {
				quitBut.addEventListener(MouseEvent.CLICK, quitBut_onClick, false, 0, true);
				quitBut.addEventListener(MouseEvent.ROLL_OUT, but_onRollout, false, 0, true);
				quitBut.addEventListener(MouseEvent.ROLL_OVER, but_onRollover, false, 0, true);
			}
			if (prevBut) {
				prevBut.addEventListener(MouseEvent.CLICK, prevBut_onClick, false, 0, true);
				prevBut.addEventListener(MouseEvent.ROLL_OUT, but_onRollout, false, 0, true);
				prevBut.addEventListener(MouseEvent.ROLL_OVER, but_onRollover, false, 0, true);
			}
			if (nextBut) {
				nextBut.addEventListener(MouseEvent.CLICK, nextBut_onClick, false, 0, true);
				nextBut.addEventListener(MouseEvent.ROLL_OUT, but_onRollout, false, 0, true);
				nextBut.addEventListener(MouseEvent.ROLL_OVER, but_onRollover, false, 0, true);
			}
			if (playBut) {
				playBut.addEventListener(MouseEvent.CLICK, playBut_onPlay, false, 0, true);
				playBut.addEventListener(MouseEvent.ROLL_OUT, but_onRollout, false, 0, true);
				playBut.addEventListener(MouseEvent.ROLL_OVER, but_onRollover, false, 0, true);
			}
			if(stopBut) stopBut.addEventListener(MouseEvent.CLICK, stopBut_onStop,false,0,true);
			if (replayBut) {
				replayBut.useHandCursor = true;
				replayBut.addEventListener(MouseEvent.CLICK, replayBut_onReplay,false,0,true);
				replayBut.addEventListener(MouseEvent.ROLL_OUT, but_onRollout,false,0,true);
				replayBut.addEventListener(MouseEvent.ROLL_OVER, but_onRollover,false,0,true);
			}
			if (muteBut) {
				muteBut.useHandCursor = true;
				muteBut.addEventListener(MouseEvent.CLICK, muteBut_onClick,false,0,true);
				muteBut.addEventListener(MouseEvent.ROLL_OUT, but_onRollout,false,0,true);
				muteBut.addEventListener(MouseEvent.ROLL_OVER, but_onRollover,false,0,true);
			}
			if (voTextBut) {
				voTextBut.addEventListener(MouseEvent.CLICK, voTextBut_onClick, false, 0, true);
				voTextBut.addEventListener(MouseEvent.ROLL_OUT, but_onRollout,false,0,true);
				voTextBut.addEventListener(MouseEvent.ROLL_OVER, but_onRollover,false,0,true);;
			}
			if(showMeBut) showMeBut.addEventListener(MouseEvent.CLICK, showMeBut_onClick,false,0,true);
			if (autoTransitionChkBox) autoTransitionChkBox.addEventListener(MouseEvent.CLICK, autoTransitionChkBox_onClick, false, 0, true);
			
			if(legalLink) legalLink.addEventListener(MouseEvent.CLICK, legalLink_onClick,false,0,true);
		}
		
		private function autoTransitionChkBox_onClick(e:MouseEvent):void 
		{
			var evChkBx:UIEvent = new UIEvent(UIEvent.CLICKED);
			evChkBx.id = (e.target.selected)?"auto-slide-transition-on":"auto-slide-transition-off";
			dispatchEvent(evChkBx);
		}
		
		private function voTextBut_onClick(e:MouseEvent):void 
		{
			var evVOTxt:UIEvent = new UIEvent(UIEvent.CLICKED);
			isShowingVOText = !isShowingVOText;
			evVOTxt.id = (isShowingVOText)?"votext_on":"votext_off";
			dispatchEvent(evVOTxt);
			
			//didn't see much point in handling this in lessonStateModel...
			(!isShowingVOText)?voTextBut.gotoAndStop("text_off_up"):voTextBut.gotoAndStop("text_on_up");
		}
		
		private function legalLink_onClick(e:MouseEvent):void {
			var evLegal:UIEvent = new UIEvent(UIEvent.CLICKED);
			evLegal.id = "show_legal";
			
			dispatchEvent(evLegal);
		}
		
		private function muteBut_onClick(e:MouseEvent):void 
		{
			var evMute:UIEvent = new UIEvent(UIEvent.CLICKED);
			isMuted = !isMuted;
			evMute.id = (isMuted)?"sound_off":"sound_on";
			dispatchEvent(evMute);
			
			//didn't see much point in handling this in lessonStateModel...
			(isMuted)?muteBut.gotoAndStop("off_up"):muteBut.gotoAndStop("on_up");
			
		}
		
		private function replayBut_onReplay(e:MouseEvent):void 
		{
			var evReplay:UIEvent = new UIEvent(UIEvent.CLICKED);
			evReplay.id = "replay";
			dispatchEvent(evReplay);
		}
		
		private function playBut_onPlay(e:MouseEvent):void 
		{
			var evPrev:UIEvent = new UIEvent(UIEvent.CLICKED);
			isPaused = !isPaused;
			evPrev.id = (!isPaused)?"pause":"play";
			dispatchEvent(evPrev);
		}
		
		private function stopBut_onStop(e:MouseEvent):void 
		{
			var evPrev:UIEvent = new UIEvent(UIEvent.CLICKED);
			evPrev.id = "stop";
			dispatchEvent(evPrev);
			
		}
		private function prevBut_onClick(e:MouseEvent):void 
		{
			var evPrev:UIEvent = new UIEvent(UIEvent.CLICKED);
			evPrev.id = "previous";
			dispatchEvent(evPrev);
		}
		
		private function quitBut_onClick(e:MouseEvent):void 
		{
			var evPrev:UIEvent = new UIEvent(UIEvent.CLICKED);
			evPrev.id = "quit";
			dispatchEvent(evPrev);
		}
		
		private function nextBut_onClick(e:MouseEvent):void 
		{
			var evNxt:UIEvent = new UIEvent(UIEvent.CLICKED);
			evNxt.id = "next";
			dispatchEvent(evNxt);		
		}
		
		private function showMeBut_onClick(e:MouseEvent):void 
		{
			var evShowMe:UIEvent = new UIEvent(UIEvent.CLICKED);
			evShowMe.id = "show_me";
			dispatchEvent(evShowMe);
		}
		
		public function prevAndNextStatus(firstSlide:Boolean, lastSlide:Boolean):void 
		{
			onFirstSlide = firstSlide;
			onLastSlide = lastSlide;
			if (prevBut) {
				prevBut.mouseEnabled = firstSlide?false:true;
				nextBut.mouseEnabled = lastSlide?false:true;
				
				//Debugger.traceOut("firstSlide " + firstSlide + "   lastSlide" + lastSlide, Debugger.WARNING);
				onFirstSlide?prevBut.gotoAndStop("dulled"):prevBut.gotoAndStop("up");
				
				if (onLastSlide) {
					//Debugger.traceOut("next should be dulled", Debugger.WARNING);
					nextBut.gotoAndStop("dulled");
				}else {
					//Debugger.traceOut("next should NOT be dulled", Debugger.WARNING);
					nextBut.gotoAndStop("up");
				}
			}
		}
		
		public function playAndStopStatus(isPlaying:Boolean, isStopped:Boolean):void 
		{
			isPaused = isPlaying;
			
			if (isPlaying) {
				playBut.gotoAndStop("pause_up");
				
			}else {
				playBut.gotoAndStop("play_up");
			}
			if (!isStopped) {
				stopBut.gotoAndStop("up");
				stopBut.mouseEnabled = true;
			}else {
				stopBut.gotoAndStop("dulled");
				stopBut.mouseEnabled = false;
			}
			
		}
		
		/**
		 * Controls should not be selectable when state is PREINITIALISED
		 */
		public function preInitialise():void 
		{
			if(showMeBut) showMeBut.visible = false;
			
			if(stopBut) stopBut.gotoAndStop("dulled");
			if(playBut) playBut.gotoAndStop("dulled");
			if(prevBut) prevBut.gotoAndStop("dulled");
			if(nextBut) nextBut.gotoAndStop("dulled");
			if(replayBut) replayBut.gotoAndStop("dulled");
			if(muteBut) muteBut.gotoAndStop("dulled");
			
			if(stopBut) stopBut.mouseEnabled = false;
			if(playBut) playBut.mouseEnabled = false;
			if(prevBut) prevBut.mouseEnabled = false;
			if(nextBut) nextBut.mouseEnabled = false;
			if(replayBut) replayBut.mouseEnabled = false;
			if(muteBut) muteBut.mouseEnabled = false;
			if(showMeBut) showMeBut.mouseEnabled = false;
			
			if (autoTransitionChkBox) autoTransitionChkBox.enableChk(false);
		}
		
		public function initialised():void 
		{
			playBut.gotoAndStop("play_up");
			replayBut.gotoAndStop("up");
			
			if (isMuted) {
				muteBut.gotoAndStop("off_up");
			}else {
				muteBut.gotoAndStop("on_up");
			}
			isPaused = false;
			
			playBut.mouseEnabled = true;   
			replayBut.mouseEnabled = true;
			muteBut.mouseEnabled = true;
			
			if (autoTransitionChkBox) autoTransitionChkBox.enableChk(true);
		}
		
		public function nextSlideButtonPrompt(boo:Boolean):void 
		{
			trace("nextSlideButtonPrompt");
			if (boo) nextBut.gotoAndStop("autoPrompt");
		}
		
		public function autoPlaythrough(boo:Boolean):void 
		{
			isAutoTransitioning = boo;
			if (autoTransitionChkBox) autoTransitionChkBox.selected = isAutoTransitioning;
		}
		
		public function hideQuit(boo:Boolean):void 
		{			
			if (quitBut) quitBut.visible = !boo;
		}
		
		
		public function hidePlaythroughControl(boo:Boolean):void 
		{
			if (boo) autoTransitionMc.visible = false;
		}
		
		public function enableVoText(boo:Boolean):void 
		{
			(!boo)?voTextBut.gotoAndStop("text_off_up"):voTextBut.gotoAndStop("text_on_up");
			
			 muteBut.visible = voTextBut.visible = boo;
		}
		
		public function enableShowMe(bool:Boolean):void 
		{
			showMeBut.mouseEnabled = bool;
			showMeBut.visible = bool;
			
			if (showMeBut) {
				//place showme button above content swf
				showMeBut.parent.removeChild(showMeBut);
				this.getAppRoot().addChild(showMeBut);
			}
		}
		
		public function enablePlayandStop(boo:Boolean):void 
		{
		
			if (boo) {
				playBut.gotoAndStop("play_up");
				stopBut.gotoAndStop("up");
				stopBut.mouseEnabled = true;
				playBut.mouseEnabled = true;
			}else {
				playBut.gotoAndStop("dulled");
				stopBut.gotoAndStop("dulled");
				stopBut.mouseEnabled = false;
				playBut.mouseEnabled = false;
			}
		}
		
		//rollover/out code
		
		
		private function but_onRollover(e:MouseEvent):void 
		{
			var mc:MovieClip = e.target as MovieClip;
			switch (mc.name) 
			{
				case "voTextBut":
					if (isShowingVOText) {
						changeButtonState(mc, "text_on_over", voOffButTxt);
					}else {
						changeButtonState(mc, "text_off_over", voOnButTxt);
					}
					break;
				case "mcQuit":
					changeButtonState(mc, "over", quitButTxt);
										
					break;
				case "mcPrv":
					if (!onFirstSlide) {
						changeButtonState(mc, "over", prevButTxt);
					}

					break;
				case "mcNxt":
					if (!onLastSlide) {
						changeButtonState(mc, "over", nextButTxt);
					}
					break;
				case "mcReplay":
					changeButtonState(mc, "over", replayButTxt);
					break;
				case "mcPause":
					if (isPaused) {
						changeButtonState(mc, "pause_over", pauseButTxt);
					}else {
						changeButtonState(mc, "play_over", playButTxt);
					}
					break;
				case "mcAudio":
					if (isMuted) {
						changeButtonState(mc, "off_over", unmuteButTxt);
					}else {
						changeButtonState(mc, "on_over", muteButTxt);
					}
					break;
			}
		}
		
		private function changeButtonState(mc:MovieClip, frameLabel:String, altTxt:String):void 
		{
			mc.gotoAndStop(frameLabel);
			//mc.txt_label.text = replayButTxt;
			//TODO is there a check to see if textRender has already been created for txt_label?
			TextRender.createLabel(mc.mcLabel.txt_label, altTxt); 
		}
		
		private function but_onRollout(e:MouseEvent):void 
		{
			var mc:MovieClip = e.target as MovieClip;
			
			switch (mc.name) 
			{
				case "voTextBut":
					if (isShowingVOText) {
						mc.gotoAndStop("text_on_up");
					}else {
						mc.gotoAndStop("text_off_up");
					}
					break;
				case "mcPrv":
					if (!onFirstSlide) {
						mc.gotoAndStop("up");
					}
					break;
				case "mcNxt":
					if (!onLastSlide) {
						mc.gotoAndStop("up");
					}
					break;
				case "mcReplay":
					mc.gotoAndStop("up");
					break;
				case "mcPause":
					if (isPaused) {
						mc.gotoAndStop("pause_up");
						
					}else {
						mc.gotoAndStop("play_up");
					}
					break;
				case "mcAudio":
					if (isMuted) {
						mc.gotoAndStop("off_up");
					}else {
						mc.gotoAndStop("on_up");
					}
					break;
				default:
					mc.gotoAndStop("up");
					break;
			}
		}
		
		
		private function destroy():void 
		{
			//remove listeners
			//set things to null
		}
		
	}
	
}